﻿namespace TPXSDK.NET.Mission;

/// <summary>
/// 万能数值类型，可将4字节数据转换为指定类型
/// </summary>
public class AnyValue
{
    private readonly byte[] _bytes = new byte[4];

    public string TypeName { get; private set; } = "NONE";

    public byte[] Bytes
    {
        get => _bytes;
        set => Array.Copy(value, _bytes, 4);
    }

    public uint U32Val
    {
        get => BitConverter.ToUInt32(_bytes, 0);
        set
        {
            TypeName = "UInt32";
            var bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, _bytes, 4);
        }
    }

    public int S32Val
    {
        get => BitConverter.ToInt32(_bytes, 0);
        set
        {
            TypeName = "Int32";
            var bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, _bytes, 4);
        }
    }

    public ushort U16Val
    {
        get => BitConverter.ToUInt16(_bytes, 0);
        set
        {
            TypeName = "UInt16";
            var bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, _bytes, 2);
        }
    }

    public short S16Val
    {
        get => BitConverter.ToInt16(_bytes, 0);
        set
        {
            TypeName = "Int16";
            var bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, _bytes, 2);
        }
    }

    public byte U8Val
    {
        get => _bytes[0];
        set
        {
            TypeName = "Byte";
            _bytes[0] = value;
        }
    }

    public sbyte S8Val
    {
        get => (sbyte)_bytes[0];
        set
        {
            TypeName = "SByte";
            _bytes[0] = (byte)value;
        }
    }

    public float FloatVal
    {
        get => BitConverter.ToSingle(_bytes, 0);
        set
        {
            TypeName = "Single";
            var bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, _bytes, 4);
        }
    }
}
